<template>
  <div class="xtx-city" @mouseleave="showOrHide = false">
    <!-- 可点击区域 点击展开城市列表 再点击关闭城市列表 -->
    <div class="select" @click="showOption">
      <span class="placeholder" v-if="!changeResult.provinceName">请选择配送地址</span>
      <span class="value" v-else>
        {{ changeResult.provinceName }} - {{ changeResult.cityName }} - {{ changeResult.countyName }}
      </span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <!-- 城市列表实现(省市区) -->
    <div class="option" v-if="showOrHide">
      <span class="ellipsis"
      v-for="i in ChinaData" :key="i.code"
      @click="changeOption(i)"
      >
        {{ i.name }}
      </span>
    </div>
  </div>
</template>
<script>
import { ref, onMounted, reactive, watch } from 'vue'
import axios from 'axios'
export default {
  name: 'XtxCity',
  setup (props, ctx) {
    const showOrHide = ref(false)
    function showOption () {
      showOrHide.value = !showOrHide.value
    }

    const changeResult = reactive({
      provinceCode: '',
      provinceName: '',
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: ''
    })

    const ChinaData = ref([])
    const areaList = ref([])
    function loadData () {
      axios({
        url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json',
        method: 'GET'
      }).then(res => {
        console.log('省市列表', res)
        ChinaData.value = res.data
        areaList.value = res.data
      })
    }
    onMounted(() => {
      loadData()
    })

    function changeOption (cur) {
      ChinaData.value = cur.areaList
      if (cur.level === 0) {
        changeResult.provinceName = cur.name
        changeResult.provinceCode = cur.code
        changeResult.cityName = ''
        changeResult.cityCode = ''
        changeResult.countyName = ''
        changeResult.countyCode = ''
      }
      if (cur.level === 1) {
        changeResult.cityName = cur.name
        changeResult.cityCode = cur.code
        changeResult.countyName = ''
        changeResult.countyCode = ''
      }
      if (cur.level === 2) {
        changeResult.countyName = cur.name
        changeResult.countyCode = cur.code
        showOption()
      }

      ctx.emit('change', changeResult)
    }

    watch(
      () => { return showOrHide.value },
      (newVal) => {
        if (newVal === false) {
          ChinaData.value = areaList.value
        }
      }
    )

    return { showOption, showOrHide, ChinaData, changeResult, changeOption }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
